﻿using UnityEngine;
using System.Collections;

public class SoundManage
{
    private static SoundManage self;

    private AudioSource[] channels;
    private AudioSource bgChannel;
    private AudioSource effectChannel;

    private bool musicState = true;

    private GameObject NetworkLayer;

    private AudioClip bgAudio;

    private string bgUrl;

    /// <summary>
    /// 是否播放音乐(0:关闭;1:播放)
    /// </summary>
    public bool MusicState
    {
        get { return musicState; }
    }

    public SoundManage()
    {
        init();
    }

    private void init()
    {
        NetworkLayer = GameObject.Find("NetworkLayer");
        if (NetworkLayer != null) channels = NetworkLayer.GetComponents<AudioSource>();
        if (channels == null) return;
        bgChannel = channels[0];
        effectChannel = channels[1];

        if (PlayerPrefs.HasKey("musicState"))
        {
            musicState = PlayerPrefs.GetInt("musicState") == 1 ? true : false;
            onMusicOnOrOff(musicState);
        }
    }

    public static SoundManage getInstance()
    {
        if (self == null)
        {
            self = new SoundManage();
        }
        return self;
    }

    /// <summary>
    /// 打开关闭音乐
    /// </summary>
    public void onMusicOnOrOff(bool val)
    {
        if (musicState == val) return;
        musicState = val;
        bgChannel.mute = !val;
        effectChannel.mute = !val;
    }

    /// <summary>
    /// 播放背景音乐
    /// </summary>
    public void playMusic(string url)
    {
        try
        {
            if (url == "") return;
            bgUrl = url;
            bgAudio = Resources.Load(ResPath.MUSIC + bgUrl) as AudioClip;
            if (bgAudio == null) return;
            if (bgChannel != null && bgChannel.isPlaying)
            {
                stopBG(url);
            }
            else
            {
                bgChannel.clip = bgAudio;
                bgChannel.volume = 1;
                bgChannel.loop = true;
                bgChannel.Play();
            }
        }
        catch (System.Exception ex)
        {
            Debug.Log("playMusic " + ex.ToString());
        }

    }

    /// <summary>
    /// 播放特效音乐
    /// </summary>
    public void playEffect(string url)
    {
        if (url == "") return;
        if (effectChannel != null && effectChannel.isPlaying) effectChannel.Stop();

        AudioClip audio = Resources.Load(ResPath.MUSIC + url) as AudioClip;

        effectChannel.clip = audio;
        effectChannel.loop = false;
        effectChannel.Play();
    }


    /// <summary>
    /// 切场景时停止背景
    /// </summary>
    public void stopBG()
    {
        if (bgChannel != null && bgChannel.isPlaying)
        {
            bgUrl = "";
            setVolume(bgChannel, 0, "");
        }
    }

    private void stopBG(string url)
    {
        if (bgChannel != null && bgChannel.isPlaying)
        {
            setVolume(bgChannel, 0, url);
        }
    }

    private void setVolume(AudioSource audio, float volume, string url)
    {
        Hashtable args = new Hashtable();
        args.Add("audiosource", audio);
        args.Add("volume", volume);
        args.Add("oncomplete", "AnimationEnd");
        args.Add("oncompletetarget", this);
        args.Add("time", 4);
        iTween.AudioTo(NetworkLayer, args);
    }

    public void AnimationEnd(GameObject obj)
    {
        if (bgUrl != "")
        {
            bgChannel.clip = bgAudio;
            bgChannel.volume = 1;
            bgChannel.loop = true;
            bgChannel.Play();
        }
    }


}